package com.tencent.bk.utils.blueking.tools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by sillyboy on 2017/9/4
 */
public class AnalysisObjectList {
    private final static Logger logger = LoggerFactory.getLogger(AnalysisObjectList.class);

    public static String formatDouble(double d) {
        DecimalFormat df = new DecimalFormat("########0.00");
        return df.format(d);
    }

    public static String formatMoney(double d) {
        DecimalFormat df = new DecimalFormat("########0.0");
        return df.format(d);
    }

    public static String formatDouble3(double d) {
        DecimalFormat df = new DecimalFormat("########0.000");
        return df.format(d);
    }

    public static Double formatDoubleToDouble(double d) {
        DecimalFormat df = new DecimalFormat("########0.00");
        return StringConverters.ToDouble(df.format(d));
    }

    public static JSONObject mapGetJsonObject(Map<String, JSONObject> map, String key) {
        try {
            return map.get(key);
        } catch (Exception ignored) {
        }
        return new JSONObject();
    }

    public static String getJsonObjectString(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getString(key);
        } catch (Exception ignored) {
        }
        return "";
    }

    public static JSONArray getJsonArray(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getJSONArray(key);
        } catch (Exception ignored) {
        }
        return null;
    }


    public static JSONObject getJsonObject(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getJSONObject(key);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static String ObjectString(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getString(key);
        } catch (Exception ignored) {
        }
        return "";
    }

    public static Long ObjectLong(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getLong(key);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static Double ObjectDouble(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getDouble(key);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static Integer ObjectInteger(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getInteger(key);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static Integer androidResultStatus(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getInteger(key);
        } catch (Exception ignored) {
        }
        return 0;
    }

    public static String getJsonString(JSONArray ja, Integer index) {
        return "null".equals(ja.getString(index)) ? "" : ja.getString(index);
    }

    public static Long getJsonLong(JSONArray ja, Integer index) {
        return "null".equals(ja.getString(index)) ? 0L : ja.getLong(index);
    }

    public static Integer getJsonInteger(JSONArray ja, Integer index) {
        return "null".equals(ja.getString(index)) ? 0 : ja.getInteger(index);
    }

    public static Double getJsonDouble(JSONArray ja, Integer index) {
        return "null".equals(ja.getString(index)) ? 0D : ja.getDouble(index);
    }

    public static Boolean getJsonToBoolean(JSONArray ja, Integer index) {
        String result = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
        if ("true".equals(result)) {
            return true;
        }
        return false;
    }

    public static String getJsonToDoubleString(JSONArray ja, Integer index) {
        String money = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
        return StringUtils.isEmpty(money) ? "0.00" : formatDouble(StringConverters.ToDouble(money));
    }

    public static String getJsonToDouble3String(JSONArray ja, Integer index) {
        String money = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
        return StringUtils.isEmpty(money) ? "0.000" : formatDouble3(StringConverters.ToDouble(money));
    }

    public static String getJsonToIntegerString(JSONArray ja, Integer index) {
        String money = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
        return StringUtils.isEmpty(money) ? "0" : money;
    }

    public static String getJsonToTimeString(JSONArray ja, Integer index) {
        try {
            String timeStamp = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
            String time = net.sf.json.JSONObject.fromObject(timeStamp).getString("time");
            return StringUtils.isEmpty(time) ? "" : stampToDate(time, "yyyy-MM-dd HH:mm");
        } catch (Exception e) {
            return "";
        }
    }

    public static String getJsonToDateString(JSONArray ja, Integer index) {
        try {
            String timeStamp = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
            String time = net.sf.json.JSONObject.fromObject(timeStamp).getString("time");
            return StringUtils.isEmpty(time) ? "" : stampToDate(time, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            return "";
        }
    }

    public static String getJsonToDateParamString(JSONArray ja, Integer index, String stampParam) {
        try {
            String timeStamp = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
            String time = net.sf.json.JSONObject.fromObject(timeStamp).getString("time");
            return StringUtils.isEmpty(time) ? "" : stampToDate(time, stampParam);
        } catch (Exception e) {
            return "";
        }
    }

    public static Integer formatDoubleToInteger(double d) {
        DecimalFormat df = new DecimalFormat("########0");
        return StringConverters.ToInteger(df.format(d));
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s, String formatParam) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatParam);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     *计算time2减去time1的差值 几秒
     * 根据差值返回多长之间前或多长时间后
     * */
    public static long getDistanceTime(long time1, long time2) {
        long diff;

        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        return diff / 1000;
    }

    /*
     *计算time2减去time1的差值 几分钟
     * 根据差值返回多长之间前或多长时间后
     * */
    public static long getDistanceMinuteTime(long time1, long time2) {
        long diff;

        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        return diff / 1000 / 60;
    }

    //解析请求参数
    public static Map<String, String> analysisRequestParam(String myParam) {
        Map<String, String> map = new HashMap<>();
        if (StringUtils.isBlank(myParam) || "null".equals(myParam)) {
            return map;
        }
        String[] myParams = myParam.split("\\*");
        for (String param : myParams) {
            if (param.split("=").length >= 2) {
                map.put(param.split("=")[0], param.split("=")[1]);
            }
        }
        return map;
    }

    public static Boolean isPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return false;
        }
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            return m.matches();
        }
    }

    public static Boolean compare_date(Date dt1, Date dt2) {
        try {
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2后");
                return true;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2前");
                return false;
            } else {
                return true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }
}

